#include "PCH.h"

core::position2di pos;
bool mousedown = false;

class MyEventReceiver : public IEventReceiver
{
private:
	device::DeviceWin32* m_pDevice;

public:
	MyEventReceiver(device::DeviceWin32* pDevice) : m_pDevice(pDevice) {}

	virtual bool OnEvent(const SEvent& event)
	{
		if (event.type == EET_KeyInput)
		{
			if (event.key.pressedKey)
			{
				switch (event.key.key)
				{
				case KEY_1 :
					m_pDevice->restoreWindow();
					break;

				case KEY_2 :
					m_pDevice->maximizeWindow();
					break;

				case KEY_3 :
					m_pDevice->minimizeWindow();
					break;

				case KEY_ESCAPE :
					m_pDevice->close();
					break;
				}
			}
		}
		else if (event.type == EET_MouseInput)
		{
			pos = core::position2di(event.mouse.x, event.mouse.y);

			switch (event.mouse.event)
			{
			case EMIE_LButtonDown :
				mousedown = true;
				break;

			case EMIE_LButtonUp :
				mousedown = false;
				break;
			}
		}

		return true;
	}
};

void main()
{
	//// initailize ////////////////////////
	device::SDeviceParameters param;
	param.strWindowName = _T("test device");
	device::DeviceWin32 window(param);

//	graphics::IPainter* painter = window.createPainter(graphics::EPT_Win32, graphics::ECF_R8G8B8);
	graphics::IRenderer* renderer = window.createRenderer();

	MyEventReceiver receiver(&window);
	window.setEventReceiver(&receiver);

	util::FrameCounter timer;

//	core::position2di before;
//	bool readyshot = false;
//
//	std::list<entity::TestEntity> entities;
//	entity::TestEntity* curr = nullptr;
//
//	graphics::FreeImageLoader loader;
//	graphics::IImage* image = loader.load(_T("bubble1.bmp"));
//	/////////////////////////////////////////////

	while (window.run())
	{
//		//// input ////////////////////////
//		if (mousedown)
//		{
//			if (!readyshot)
//			{
//				before = pos;
//				readyshot = true;
//			}
//		}
//		else
//		{
//			if (readyshot)
//			{
//				core::vector2df dir = before - pos;
//				entities.push_back(*new entity::TestEntity(before, dir.normalize() * 50.0f, image));
//				before = core::position2di(0);
//				readyshot = false;
//			}
//		}
//		//////////////////////////////////

//		painter->preDraw();
		renderer->beginScene();

//		//// update /////////////////////////
//		static u32 beforeMs = 0;
//		u32 timeMs = util::TimeManager::getTime32();
//		u32 deltaMs = timeMs - beforeMs;
//
//		if (!before.isZero())
//			painter->drawLine(before, pos);
//
//		if (!entities.empty())
//		{
//			auto update = [=](entity::TestEntity& entity)
//			{
//				entity.update(deltaMs);
//				entity.render(painter);
//			};
//
//			std::for_each(entities.begin(), entities.end(), update);
//		}
//
//		beforeMs = timeMs;
//		/////////////////////////////////////

//		painter->postDraw();
		renderer->endScene();

		//// frame counting ////////////////// 
		static u32 beforeFPS = 0;
		timer.countFrame();
		u32 fps = timer.getFPS();

		if (beforeFPS != fps)
		{
			std::tstringstream t;
			t << "fps : " << fps;
			window.setWindowCaption(t.str());

			beforeFPS = fps;
		}
		////////////////////////////////////////
	}

//	//// terminate ////////////////////////
//	if (painter)
//		painter->detach();
//
//	if (image)
//		image->detach();
//	/////////////////////////////////////
}
